enum ServoPIN {
    //% block="S1"
    8,
    //% block="S2"
    9,
    //% block="S3"
    10,
    //% block="S4"
    11,
    //% block="S5"
    12,
    //% block="S6"
    13,
    //% block="S7"
    14,
    //% block="S8"
    15	
}

enum MotoPIN {
    //% block="M1A"
    M1A,
    //% block="M1B"
    M1B,
    //% block="M2A"
    M2A,
    //% block="M2B"
    M2B	
}

enum MOTOR {
    //% block="M1A"
    M1A,
    //% block="M1B"
    M1B,
    //% block="M2A"
    M2A,
    //% block="M2B"
    M2B,
    //% block="ALL"
    ALL	
}

enum Reverse {
    //% block="false"
    false,
    //% block="true"
    true
}

//% color="#4169E1" iconWidth=50 iconHeight=40
namespace Robotbit{
    // 初始化代码提取到单独的函数
    function initializePWM() {
        Generator.addInclude("16ServoRobotbit","#include <Adafruit_PWMServoDriver.h>");
        Generator.addObject(`16ServoRobotbit1` ,`Adafruit_PWMServoDriver`,`pwm1 = Adafruit_PWMServoDriver(0x40);`);        
        Generator.addSetup(`16ServoRobotbit`,`pwm1.begin();`);      
        Generator.addSetup(`16ServoRobotbit1`,`pwm1.setPWMFreq(50);`);	
    }
    
    //% block="Robotbit Servo PIN [PIN]ANGLE [ANGLE] " blockType="command"
    //% PIN.shadow="dropdownRound" PIN.options="ServoPIN"    
    //% ANGLE.shadow="range"  ANGLE.params.min=0    ANGLE.params.max=180  ANGLE.defl=90
    export function ServoAngle(parameter: any, block: any) {        
      initializePWM();
      let pin = parameter.PIN.code;         
      let angle = parameter.ANGLE.code;
      Generator.addCode(`pwm1.writeServo(${pin},${angle});`);                 
    }
	  
    //% block="Robotbit Moto PIN [PIN] PWM [PWM] Reverse [Reverse]" blockType="command" 
    //% PIN.shadow="dropdownRound" PIN.options="MotoPIN"    
    //% PWM.shadow="range"  PWM.params.min=0    PWM.params.max=4095    PWM.defl=2048
    //% Reverse.shadow="dropdownRound" Reverse.options="Reverse"	
    export function MotorRotation(parameter: any, block: any) {        
      initializePWM();
      let pin = parameter.PIN.code;         
      let pwm = parameter.PWM.code;
      let reverse = parameter.Reverse.code; // 直接使用参数值
	  
      // 根据PIN和Reverse参数生成对应的PWM控制代码
      let codeLines = [];
      
      if (reverse === 'true') {
        // 反向逻辑：交换每对引脚的顺序
        switch(pin) {
          case 'M1A':
            codeLines.push(`pwm1.setPWM(1, 0, 0);`);
            codeLines.push(`pwm1.setPWM(0, 0, ${pwm});`);
            break;
          case 'M1B':
            codeLines.push(`pwm1.setPWM(3, 0, 0);`);
            codeLines.push(`pwm1.setPWM(2, 0, ${pwm});`);
            break;
          case 'M2A':
            codeLines.push(`pwm1.setPWM(5, 0, 0);`);
            codeLines.push(`pwm1.setPWM(4, 0, ${pwm});`);
            break;
          case 'M2B':
            codeLines.push(`pwm1.setPWM(7, 0, 0);`);
            codeLines.push(`pwm1.setPWM(6, 0, ${pwm});`);
            break;
        }
      } else {
        // 正向逻辑：原始引脚顺序
        switch(pin) {
          case 'M1A':
            codeLines.push(`pwm1.setPWM(0, 0, 0);`);
            codeLines.push(`pwm1.setPWM(1, 0, ${pwm});`);
            break;
          case 'M1B':
            codeLines.push(`pwm1.setPWM(2, 0, 0);`);
            codeLines.push(`pwm1.setPWM(3, 0, ${pwm});`);
            break;
          case 'M2A':
            codeLines.push(`pwm1.setPWM(4, 0, 0);`);
            codeLines.push(`pwm1.setPWM(5, 0, ${pwm});`);
            break;
          case 'M2B':
            codeLines.push(`pwm1.setPWM(6, 0, 0);`);
            codeLines.push(`pwm1.setPWM(7, 0, ${pwm});`);
            break;
        }
      }
	  
      // 将生成的代码行添加到代码生成器中
      codeLines.forEach(line => {
        Generator.addCode(line);
      });
    }

    //% block="Robotbit Motor [MOTOR] Stop" blockType="command" 
    //% MOTOR.shadow="dropdownRound" MOTOR.options="MOTOR"    
    export function MotorStop(parameter: any, block: any) {        
      initializePWM();
      let motor = parameter.MOTOR.code; // 修复：使用motor变量而不是pin
	  
      // 根据MOTOR参数生成对应的PWM控制代码
      let codeLines = [];
      
      switch(motor) { // 修复：使用motor变量
        case 'M1A':
          codeLines.push(`pwm1.setPWM(1, 0, 0);`);
          codeLines.push(`pwm1.setPWM(0, 0, 0);`);
          break;
        case 'M1B':
          codeLines.push(`pwm1.setPWM(3, 0, 0);`);
          codeLines.push(`pwm1.setPWM(2, 0, 0);`);
          break;
        case 'M2A':
          codeLines.push(`pwm1.setPWM(5, 0, 0);`);
          codeLines.push(`pwm1.setPWM(4, 0, 0);`);
          break;
        case 'M2B':
          codeLines.push(`pwm1.setPWM(7, 0, 0);`);
          codeLines.push(`pwm1.setPWM(6, 0, 0);`);
          break;
        case 'ALL':
          codeLines.push(`pwm1.setPWM(0, 0, 0);`);
          codeLines.push(`pwm1.setPWM(1, 0, 0);`);
          codeLines.push(`pwm1.setPWM(2, 0, 0);`);
          codeLines.push(`pwm1.setPWM(3, 0, 0);`);
          codeLines.push(`pwm1.setPWM(4, 0, 0);`);
          codeLines.push(`pwm1.setPWM(5, 0, 0);`);
          codeLines.push(`pwm1.setPWM(6, 0, 0);`);
          codeLines.push(`pwm1.setPWM(7, 0, 0);`);
          break;
      }
	  
      // 将生成的代码行添加到代码生成器中
      codeLines.forEach(line => {
        Generator.addCode(line);
      });
    }	  
}